﻿using EmbeddedSharp.Core;
using System;
using System.Collections.Generic;

namespace EmbeddedSharp.Scripting.Function.List {

    /// <summary>
    /// lst = [1,2,3,4,5];
    /// length = length(lst);
    /// echo(length);
    /// </summary>
    public class CountNode : ContextFuncNode {

        public CountNode()
            : base() {
            this.Value = "count";
        }

        public override object Evaluate() {
            if (Parameters[0] is ListNode) {
                return (Parameters[0] as FuncNode).Parameters.Count;
            }
            string varName = Parameters[0].ToString();
            List<Object> node = base.Context.GetVariable(
                varName) as List<Object>;
            if (node == null) {
                throw new ArgumentException(
                    "Please specify a list as the argument.");
            }

            return node.Count;
        }
    }
}
